<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/


header("Content-type: image/jpeg");

$thumb_x = 133;
$thumb_y = 100;
if (isset($_GET['x'])) $thumb_x = intval ($_GET['x']);
if (isset($_GET['y'])) $thumb_y = intval ($_GET['y']);
if ($thumb_x > 1000) $thumb_x = 1000;
if ($thumb_y > 1500) $thumb_y = 1500;
if ($_GET['url'] == "") error ("None image URL!");
$src = $_GET['url'];

//if (!file_exists($src)) error ("Image not exists!");

if(eregi('[A-Za-z0-9]\.(jpeg|jpg|JPEG|JPG)', $src)) {
	$thumbbase = imagecreatefromjpeg($src);
}
elseif(eregi('[A-Za-z0-9]\.(gif|GIF)', $src)) {
	$thumbbase = imagecreatefromgif($src);
}
elseif(eregi('[A-Za-z0-9]\.(png|PNG)', $src)) {
	$thumbbase = imagecreatefrompng($src);
}
elseif(eregi('[A-Za-z0-9]\.(bmp|wbmp|BMP|wbmp)', $src)) {
	$thumbbase = imagecreatefromwbmp($src);
}
else error ("Such format of image not supported!");

$width = imagesx($thumbbase);
$height = imagesy($thumbbase);

if ($width > $thumb_x || $height > $thumb_x) {
	$k = max($width/$thumb_x, $height/$thumb_x);
	$new_width = intval($width / $k);
	$new_height = intval($height / $k);
}
else {
	$new_width = $width;
	$new_height = $height;
}

$thumb = imagecreatetruecolor ($new_width, $new_height);
imagecopyresampled($thumb, $thumbbase, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, null, 90);
imagedestroy($thumb);
imagedestroy($thumbbase);

function error ($string, $string2 = "") {
	$image = imagecreate (300, 100);
	imagecolorallocate ($image, 255, 255, 255);
	$black = imagecolorallocate ($image, 0, 0, 0);
	imagestring ($image, 20, 10, 10, "Image error:", $black);
	imagestring ($image, 2, 10, 30, $string, $black);
	imagestring ($image, 2, 10, 40, $string2, $black);
	imageline ($image, 0, 70, 300, 70, $black);
	imagestring ($image, 2, 10, 80, "http://".$_SERVER['HTTP_HOST'], $black);
	imagejpeg ($image, null, 90);
	imagedestroy ($image);
	exit;
}

?>
